﻿using System;
using System.Web.UI.WebControls;
using ElectronStoreDAL;
using System.Drawing;

namespace ElectronStore
{
	public partial class Pager : System.Web.UI.UserControl
	{
		private string strUrl;
		private int idCategory = 1;
		private int countItem = 6;
		private int indexPage = 1;

		public string StrUrl
		{
			get { return strUrl; }
			set { strUrl = value; }
		}

		public int IdCategory
		{
			get { return idCategory; }
			set { idCategory = value; }
		}

		public int CountItem
		{
			get { return countItem; }
			set { countItem = value; }
		}

		public int IndexPage
		{
			get { return indexPage; }
			set { indexPage = value; }
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(StrUrl))
				return;

			ElectronStoreSource dataSource = new ElectronStoreSource();
			//Create data pager
			HyperLink link;
			int countProducts = dataSource.CountProduct(idCategory);
			if (countProducts > 0)
			{
				int countButton = countProducts / countItem;
				if (countProducts % countItem > 0)
					countButton++;

				for (int i = 1; i <= countButton; i++)
				{
					if (i == indexPage)
					{
						Label lbl = new Label();
						lbl.Text = i.ToString();
						lbl.Attributes["Font-Bold"] = "true";
						lbl.ForeColor = Color.FromArgb(81, 147, 185);
						divMain.Controls.Add(lbl);
						continue;
					}
					link = new HyperLink();
					link.Text = i.ToString();
					link.NavigateUrl = string.Format("{0}?id={1}&page={2}", StrUrl, idCategory, i);
					link.Style.Add("margin", "3px");
					divMain.Controls.Add(link);
				}
			}
		}

	}
}